package net.zjitc.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/9/21.
 */
/*实现Servlet接口*/
//aservlet
public class YiServlet implements Servlet {
    //第一步：定义一个成员变量servletConfig,所有方法都可以使用
    private ServletConfig servletConfig=null;
    /**
     * 默认第一次访问对时候出生
     * @param servletConfig
     * @throws ServletException
     * */
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
     //第二步：初始化时赋值servletConfig;
        this.servletConfig=servletConfig;
        System.out.println("堕入人间");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    /**
     * 每次有请求，都由此方法处理
     * @param servletRequest
     * @param  servletResponse
     * @throws ServletException
     * @throws IOException
     * */

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //第三步：读取servletConfig相关信息，输出到页面
        String value=this.servletConfig.getInitParameter("Ecoding");
        PrintWriter out=servletResponse.getWriter();
        out.write("aServlet.....<br>");
        /*out.write("<h1>"+"hehehehhehehehhe"+"</h1>");
        System.out.println("hhhhhh");*/
        ServletContext context= this.servletConfig.getServletContext();
        context.setAttribute("name","zhangsan");

    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("天堂在召唤你！");
    }
}
